/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import java.io.*;
import fr.inria.openmath.omapi.*;
import fr.inria.openmath.omapi.implementation.*;
import fr.ove.openmath.mathematica.omparser.PipeParserHandler;

public class ResponseReader {
    /**
    * The XML parser.
    */
    private XMLParser parser;
    
    /**
    * The handler for the parser.
    */
    private PipeParserHandler handler = new PipeParserHandler();
    
    /**
    * The Constructor.
    */
    public ResponseReader() {
        parser = new XMLParser(handler);
    }
    
    /**
    * read the result from the input stream.
    */
    public String read(InputStream inputStream) {
        try {
            parser.initParse(inputStream);
                    
            int dispo = 0;
            while ((dispo = inputStream.available()) == 0) {
                try {
                    Thread.sleep(100);
                }
                catch (Exception e) {
                }
            }
                    
            parser.parseObject(inputStream);
                    
            return handler.getOmObjectParsed();
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}